<div class="layui-form seller-form" action="">
    <div class="layui-form-item">

        <div class="layui-inline">
            <label class="layui-form-label">链接名称：</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="title" placeholder="请输入链接名称关键字" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">时间：</label>
            <div class="layui-input-inline">
                <input type="text" id="udate" name="date" lay-verify="title" placeholder="请选择时间段" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <button type="button" class="layui-btn layui-btn-sm" lay-submit lay-filter="search"><i class="iconfont icon-chaxun"></i>筛选</button>

            <button type="button" class="layui-btn layui-btn-sm add"><i class="layui-icon">&#xe608;</i> 添加</button>
        </div>
    </div>
</div>

<table id="linkTable" lay-filter="test"></table>

<script>
    layui.use(['table', 'form', 'layer', 'laydate'], function() {
        var table = layui.table,
            form = layui.form,
            layer = layui.layer,
            date = layui.laydate;
        table.render({
            elem: '#linkTable',
            height: 'full-320',
            cellMinWidth: '80',
            page: 'true',
            limit: '20',
            url: "{:url('Link/index')}", //数据接口
            cols: [
                [ //表头
                    {
                        type: 'numbers'
                    }, {
                        field: 'name',
                        title: '链接名称',
                        width: 200,
                        align: 'center'
                    }, {
                        field: 'image',
                        title: '链接LOGO',
                        width: 120,
                        align: 'center',
                        templet: function(data) {
                            return '<a href="javascript:void(0);" onclick=viewImage("' + data.image + '")><image style="max-width:30px;max-height:30px;" src="' + data.image + '"/></a>';
                        }
                    }, {
                        field: 'ctime',
                        title: '创建时间',
                        sort: true,
                        align: 'center'
                    }, {
                        field: 'utime',
                        title: '更新时间',
                        sort: true,
                        align: 'center'
                    }, {
                        field: 'sort',
                        title: '排序',
                        sort: true,
                        align: 'center'
                    }, {
                        fixed: 'right',
                        width: 150,
                        title: '操作',
                        align: 'center',
                        toolbar: '#linkTableBar'
                    }
                ]
            ]
        });

        date.render({
            elem: '#udate',
            range: '~'
        });

        //搜索
        form.on('submit(search)', function(data) {
            layui.table.reload('linkTable', {
                where: data.field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        // 添加
        $(".add").click(function() {
            JsGet("{:url('Link/add')}", function(res) {
                window.box = layer.open({
                    type: 1,
                    title: '添加链接',
                    area: ['450px', '500px'],
                    content: res
                })
            })
        })

        form.on('submit(addHandler)', function(data) {
            JsPost("{:url('Link/add')}", data.field, function(res) {
                if (res.status) {
                    layer.msg(res.msg, {
                        time: 1300
                    }, function() {
                        layer.close(window.box);
                        table.reload('linkTable')
                    });
                } else {
                    layer.msg(res.msg);
                }
            })
        })

        form.on('submit(editHandler)', function(data) {
            JsPost("{:url('Link/edit')}", data.field, function(res) {
                if (res.status) {
                    layer.msg(res.msg, {
                        time: 1300
                    }, function() {
                        layer.close(window.box);
                        table.reload('linkTable')
                    });
                } else {
                    layer.msg(res.msg);
                }
            })
        })


        table.on('tool(test)', function(obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'del') { //删除
                layer.confirm('真的删除行么', {
                    icon: 3
                }, function(index) {
                    JsGet("{:url('Link/del')}?id=" + data.id, function(res) {
                        if (res.status) {
                            obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            layer.close(index);
                        } else {
                            layer.msg(res.msg)
                        }
                    })
                });
            } else if (layEvent === 'edit') { //编辑
                JsGet("{:url('Link/edit')}?id=" + data.id, function(res) {
                    window.box = layer.open({
                        type: 1,
                        title: '编辑友情链接',
                        area: ['450px', '500px'],
                        content: res
                    })
                })
            }
        });

    });
</script>

<script type="text/html" id="linkTableBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

    <!-- 这里同样支持 laytpl 语法，如： -->
</script>